import request from '@/router/axios';
import {
    userInfo,
    tableData,
} from '@/mock/user';
import {
    menu,
    menuAll,
} from '@/mock/menu';
import {
    baseUrl,
    wechatUrl,
    mockUrl,
} from '@/config/env';
import website from '@/const/website';


// 0 短信分享
// 1 微信分享
export const shareMsg = (userName, phone, shareType) => request({
    url: `${baseUrl}/entrance_guard/api/shareQr`,
    method: 'post',
    data: {
        userName,
        phone,
        shareType,
    },
});

export const sendMobileCode = iphone => request({
    url: `${baseUrl}/app/appCode`,
    method: 'get',
    params: {
        mobile: iphone,
    },
});
export const signIn = (username, mobileCode, openId, loginType) => request({
    url: `${baseUrl}/guard/signIn`,
    method: 'post',
    data: {
        username,
        mobileCode,
        openId,
        loginType,
    },
});
export const loginByWechat = code => request({
    url: `${wechatUrl}/token/getUserInfo`,
    method: 'get',
    headers: {
        appId: website.appId,
    },
    params: {
        code,
    },
});


export const wxunbind = () => request({
    url: `${baseUrl}/app/unBindOpenId`,
    method: 'get',
});

export const invitationbind = data => request({
    url: `${baseUrl}/app/bindOwner/doAdd`,
    method: 'post',
    data,
});


export const queryUserByUnionId = unionId => request({
    url: `${baseUrl}/auth/mobile/token`,
    method: 'post',
    headers: {
        Authorization: 'Basic YXBwX2NvbW11bml0eTphcHBfY29tbXVuaXR5',
    },
    params: {
        openId: unionId,
        grant_type: 'openId',
        scope: 'app_server',
        loginType: 1,
    },
});

export const bindUser = (unionId, mobile, code) => request({
    url: `${baseUrl}/app/bindOpenId`,
    method: 'get',
    headers: {
        Authorization: 'Basic YXBwX2NvbW11bml0eTphcHBfY29tbXVuaXR5',
    },
    params: {
        openId: unionId,
        mobile,
        code,
    },
});

export const loginByCode = (username, mobileCode) => request({
    url: `${baseUrl}/auth/mobile/token`,
    method: 'post',
    headers: {
        Authorization: 'Basic YXBwX2NvbW11bml0eTphcHBfY29tbXVuaXR5',
    },
    params: {
        mobile: username,
        code: mobileCode,
        grant_type: 'mobile',
        scope: 'app_server',
    },
});

export const refeshToken = refesh_token => request({
    url: `${baseUrl}/auth/oauth/token`,
    method: 'post',
    headers: {
        loading: false,
        auth: false,
    },
    params: {
        client_id: 'app_community',
        client_secret: 'app_community',
        grant_type: 'refresh_token',
        refresh_token: refesh_token,
    },
});

export const getCodeList = communityId => request({
    url: `${baseUrl}/entrance_guard/api/getCommunity`,
    method: 'get',
    params: {
        communityId,
    },
});

export const openCodeList = data => request({
    url: `${baseUrl}/entrance_guard/api/remoteOpenDoor`,
    method: 'post',
    data: {
        deviceId: data.deviceId,
        deviceName: data.name,
        hubCode: data.hubCode,
    },
});

export const getCode = communityId => request({
    url: `${baseUrl}/entrance_guard/api/generate`,
    method: 'get',
    params: { communityId },
});

export const queryCode = qrId => request({
    url: `${baseUrl}/entrance_guard/api/polling`,
    method: 'get',
    headers: {
        loading: false,
    },
    params: {
        qrId,
    },
});
export const getUserInfo = () => request({
    url: `${baseUrl}/app/bindOwner/currentUser`,
    method: 'get',
});
export const getWuyeAuth = () => request({
    url: `${baseUrl}/app/bindOwner/getCommunityInfosByWyId`,
    method: 'get',
});

export const setWuyeDefault = (communityId, userId) => request({
    url: `${baseUrl}/app/bindOwner/setDefaultCommunity`,
    method: 'get',
    params: {
        communityId,
        userId,
    },
});
export const getUserMenu = parentId => new Promise((resolve, reject) => {
    $.get(`${mockUrl}/api/v1/user/menu`, (res) => {
        resolve({
            data: res,
        });
    });
});
export const getMenuAll = () => new Promise((resolve, reject) => {
    resolve({
        data: menu[0],
    });
});

export const getTableData = page => new Promise((resolve, reject) => {
    resolve({
        data: tableData,
    });
});
export const logout = () => new Promise((resolve, reject) => {
    resolve();
});